package com.zklh.leetcode.top100;

import java.util.Arrays;

/**
 * 除以自身以外数组的乘积算法实现
 *
 * @version 1.0
 * @Author tongnan
 * @Date 2023/10/20
 */
public class Solution238 {
    public int[] productExceptSelf(int[] nums) {
        int[] answer = new int[nums.length];
        answer[nums.length-1] = 1;
        for (int i = nums.length-2; i >= 0; i--) {
            answer[i] = answer[i+1] * nums[i+1];
        }

        int leftProduct = 1;
        for (int i = 0; i < nums.length; i++) {
            answer[i] = leftProduct * answer[i];
            leftProduct = leftProduct * nums[i];
        }
        return answer;
    }

    public static void main(String[] args) {
        Solution238 solution238 = new Solution238();
        // int[] nums = {1,2,3,4};
        int[] nums = {-1,1,0,-3,3};
        System.out.println(Arrays.toString(solution238.productExceptSelf(nums)));
    }
}